package com.dnw.shyfunny;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dnw.util.AppUtil;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author 杨瑜
 * @description 新版本引导界面
 */
public class NewVersionGuideActivity extends Activity {

    @Bind(R.id.vp_guide)
    ViewPager vpGuide;
    private List<View> viewList;
    private int[] drawableRes = new int[]{R.drawable.nav_1,R.drawable.nav_2,
            R.drawable.nav_3, R.drawable.nav_4, R.drawable.nav_5};
    private int[] drawableResSanXing = new int[]{R.drawable.nav_1,R.drawable.nav_6,
            R.drawable.nav_3, R.drawable.nav_4, R.drawable.nav_5};
    private Bitmap b;

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_version_guide);
        ButterKnife.bind(this);
        viewList = new ArrayList<View>();
        for (int i = 0; i < 5; i++) {
            if (i == 4) {
                ImageView img = new ImageView(this);
                if(AppUtil.getChannel(NewVersionGuideActivity.this).equalsIgnoreCase("sanxing")){
                    b = readBitMap(this, drawableResSanXing[i]);
                }else{
                    b = readBitMap(this, drawableRes[i]);
                }
                img.setBackgroundDrawable(new BitmapDrawable(b));
                img.setScaleType(ImageView.ScaleType.FIT_START);
                img.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(NewVersionGuideActivity.this, MainActivity.class));
                        finish();
                    }
                });
                viewList.add(img);

            } else {
                ImageView img = new ImageView(this);
                if(AppUtil.getChannel(NewVersionGuideActivity.this).equalsIgnoreCase("sanxing")){
                    img.setBackgroundDrawable(new BitmapDrawable(readBitMap(this,
                            drawableResSanXing[i])));
                }else{
                    img.setBackgroundDrawable(new BitmapDrawable(readBitMap(this,
                            drawableRes[i])));
                }

                // img.setImageResource(drawableRes[i]);
                img.setScaleType(ImageView.ScaleType.FIT_START);
                // img.setBackgroundColor(colors[i]);
                viewList.add(img);
            }

        }

        StepPagerAdapter adpter = new StepPagerAdapter();
        vpGuide.setAdapter(adpter);
        vpGuide.setCurrentItem(0);
    }

    public class StepPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = viewList.get(position);
            if (view.getParent() != null) {
                container.removeView(view);
            }
            container.addView(view);
            return viewList.get(position);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position,
                                   Object object) {
            super.setPrimaryItem(container, position, object);
        }

        @Override
        public void startUpdate(ViewGroup container) {
            super.startUpdate(container);
        }

    }

    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    @Override
    protected void onDestroy() {
        if (b != null && !b.isRecycled())
            b.recycle();
        super.onDestroy();
    }
}
